package org.example.wx.config;

import org.example.wx.user.entity.ShippingAddress;
import org.example.wx.user.repository.ShippingAddressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private ShippingAddressRepository shippingAddressRepository;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否已有数据，如果没有则创建测试数据
        long count = shippingAddressRepository.countByUserIdAndStatus(1L);
        
        if (count == 0) {
            // 创建测试地址数据
            ShippingAddress address1 = new ShippingAddress();
            address1.setUserId(1L);
            address1.setRecipientName("测试用户0");
            address1.setPhoneNumber("17612345678");
            address1.setProvince("甘肃省");
            address1.setCity("甘南藏族自治州");
            address1.setCounty("碌曲县");
            address1.setDetailedAddress("松日鼎盛大厦0层0号");
            address1.setAddressTag("家");
            address1.setIsDefault(true);
            shippingAddressRepository.save(address1);

            ShippingAddress address2 = new ShippingAddress();
            address2.setUserId(1L);
            address2.setRecipientName("测试用户1");
            address2.setPhoneNumber("17612345678");
            address2.setProvince("甘肃省");
            address2.setCity("甘南藏族自治州");
            address2.setCounty("碌曲县");
            address2.setDetailedAddress("松日鼎盛大厦1层1号");
            address2.setAddressTag("公司");
            address2.setIsDefault(false);
            shippingAddressRepository.save(address2);

            System.out.println("✅ 测试收货地址数据已创建");
        } else {
            System.out.println("✅ 收货地址数据已存在，跳过初始化");
        }
    }
}
